import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    id: null
  },


  onLoad: function(options) {
    console.log(options, 'options')
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },
  onShow: function() {
    this.getdetail()
  },

  handleDetail: function(e) {
    console.log(e, 'e')
    wx.navigateTo({
      url: '/pages/good/detail/index?id=' + e.currentTarget.dataset.id
    })
  },

  //获取详情
  getdetail: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/detail?id=' + this.data.id,
      method: 'POST',
    }, (res) => {
      var resVo = res.vo
      if (resVo.detailJson && typeof resVo.detailJson == 'string') {
        resVo.detailJson = JSON.parse(resVo.detailJson)
      }
      var expressList=[]
      if (resVo.expressJson && typeof resVo.expressJson == 'string') {
        resVo.expressJson = JSON.parse(resVo.expressJson)
        expressList = resVo.expressJson.datas.list
      }
      
      this.setData({
        detail: resVo,
        expressList
      })

      // //发货完成
      // if (resVo.overShip == 1) {
      //   var params = {
      //     expNo: resVo.expressNo,
      //     shipperCode: resVo.shipperCode
      //   }
      //   this.logisticsTrace()
      // }

    })
  },

  wxPay: function() {
    var odrIdList = [this.data.id]
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/wxPay',
      method: 'POST',
      data: odrIdList,
    }, function(res) {
      that.requestPayment(res)
    })
  },

  //微信支付
  requestPayment: function(data) {
    var that = this;
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.pkg,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function(res) {
        console.log(res, '支付成功参数')
        setTimeout(function() {
          wx.redirectTo({
            url: '/pages/settle/success?id=' +that.data.id,
          })
        }, 1000)
      },
      'fail': function(res) {
        wx.showToast({
          icon: 'none',
          title: '支付失败',
        }) 
      },
      'complete': function(res) {
        // that.payCallBack(data.outTradeNo)
      }
    })
  },

  //物流信息
  logistics:function(e){ 
    wx.navigateTo({
      url: '/pages/logistics/index?id=' + e.currentTarget.dataset.id,
    })
  },

  //申请退款
  refund: function (e) {
    wx.navigateTo({
      url: '/pages/order/refund?id=' + e.currentTarget.dataset.id
    })
  },

  
  //评价
  assess: function (e) {
    wx.navigateTo({
      url: '/pages/order/assess?id=' + e.currentTarget.dataset.id + '&itemid=' + e.currentTarget.dataset.itemid
    })
  },

  //评价列表
  assessList: function (e) {
    wx.navigateTo({
      url: '/pages/order/assesslist?id=' + e.currentTarget.dataset.id
    })
  },
  
 
  //确认收货
  take: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '是否确定收货',
      success: function (res) {
        if (res.confirm) {
          that.confirmDeliveryReq(id)
        }
      }
    })
  },

  //确认收货请求
  confirmDeliveryReq: function (id) {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/confirmDelivery',
      method: 'POST',
      data: {
        id
      }
    }, (res) => {
      wx.showToast({
        title: '收货成功',
      })
      this.getdetail()
      // setTimeout(() => {
      //   wx.navigateTo({
      //     url: '/pages/order/assess?id=' + id
      //   }, 1500)
      // })
    })
  },


  //再次购买
  purchase: function (e) {
    var goodsOdrList = e.currentTarget.dataset.item
    var odrTotalPrice = e.currentTarget.dataset.price
    wx.setStorageSync('goodsOdrList', goodsOdrList)
    wx.setStorageSync('odrTotalPrice', odrTotalPrice)

    wx.navigateTo({
      url: '/pages/settle/index',
    })

  }



})